var { MongoClient } = require("mongodb");
//链接数据库
exports.conn = function (callback) {
    // var CONN_DB_STR = "mongodb://localhost:27017/kaiyan";
    var CONN_DB_STR = "mongodb://59.110.237.148:27017/kaiyan";
    MongoClient.connect(CONN_DB_STR, (err, db) => {
        if (err) {
            callback(err, null);
            throw err;
        } else {
            callback(null, db);
        }
    });
}
//判断数据库是否报错
exports.isError = function (err, res, db) {
    if (err) {
        res.json({
            code: 0,
            msg: "数据库错误"
        });
        db.close();
        throw err;
    }
}
//判断是否登录
exports.isLogin = function (username, res, callback) {
    if (username) {
        callback();
    } else {
        res.json({
            msg: "未登录",
            code: 0
        });
    }
}
//时间格式化
exports.dateFormat = function () {
    var date = new Date(arguments[0]);
    var str = arguments[1];
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    month = month < 10 ? "0" + month : month;
    var day = date.getDate();
    day = day < 10 ? "0" + day : day;
    var hour = date.getHours();
    hour = hour < 10 ? "0" + hour : hour;
    var min = date.getMinutes();
    min = min < 10 ? "0" + min : min;
    var sec = date.getSeconds();
    sec = sec < 10 ? "0" + sec : sec;
    return str.replace("yyyy", year).replace("mm", month).replace("dd", day).replace("hh", hour).replace("SS", min).replace("ss", sec);
}